<template>
  <div class="kanban-view">
    <div class="kanban-columns">
      <div 
        v-for="column in columns" 
        :key="column.id"
        class="kanban-column"
      >
        <div class="column-header" :class="column.id">
          <h3>{{ column.title }}</h3>
          <span class="task-count">({{ todosByStatus[column.id].length }})</span>
        </div>
        
        <div class="column-todos">
          <div 
            v-for="todo in todosByStatus[column.id]" 
            :key="todo.id"
            class="kanban-card"
            :class="{ completed: todo.completed }"
            draggable="true"
            @dragstart="onDragStart($event, todo)"
            @dragover.prevent
            @drop="onDrop($event, column.id)"
            @click="$emit('todo-clicked', todo)"
          >
            <div class="card-header">
              <h4 class="card-title">{{ todo.title }}</h4>
              <span 
                v-if="todo.priority" 
                class="priority-tag" 
                :class="todo.priority"
              >
                {{ getPriorityText(todo.priority) }}
              </span>
            </div>
            
            <div class="card-content" v-if="todo.description">
              <p class="card-description">{{ todo.description }}</p>
            </div>
            
            <div class="card-footer">
              <span 
                v-if="todo.dueDate" 
                class="due-date"
                :class="{ overdue: isOverdue(todo) }"
              >
                {{ formatDueDate(todo.dueDate) }}
              </span>
              <div class="card-tags" v-if="todo.tags && todo.tags.length">
                <span 
                  v-for="tag in todo.tags" 
                  :key="tag"
                  class="tag"
                >
                  {{ tag }}
                </span>
              </div>
            </div>
          </div>
          
          <div 
            v-if="todosByStatus[column.id].length === 0"
            class="empty-column"
          >
            暂无任务
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'KanbanView',
  props: {
    todos: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      columns: [
        { id: 'todo', title: '待办', color: 'blue' },
        { id: 'in-progress', title: '进行中', color: 'orange' },
        { id: 'completed', title: '已完成', color: 'green' }
      ],
      dragTodo: null
    }
  },
  computed: {
    todosByStatus() {
      const grouped = {
        todo: [],
        'in-progress': [],
        completed: []
      };
      
      this.todos.forEach(todo => {
        if (todo.completed) {
          grouped.completed.push(todo);
        } else if (todo.inProgress) {
          grouped['in-progress'].push(todo);
        } else {
          grouped.todo.push(todo);
        }
      });
      
      return grouped;
    }
  },
  methods: {
    onDragStart(event, todo) {
      this.dragTodo = todo;
      event.dataTransfer.effectAllowed = 'move';
    },
    onDrop(event, status) {
      event.preventDefault();
      
      if (this.dragTodo) {
        // 在实际应用中，这里应该更新任务状态
        console.log(`将任务 ${this.dragTodo.id} 移动到 ${status} 状态`);
        this.$emit('status-changed', this.dragTodo.id, status);
        this.dragTodo = null;
      }
    },
    getPriorityText(priority) {
      const priorityMap = {
        'high': '高优先级',
        'medium': '中优先级',
        'low': '低优先级'
      };
      return priorityMap[priority] || priority;
    },
    formatDueDate(dueDate) {
      if (!dueDate) return '';
      const date = new Date(dueDate);
      const now = new Date();
      
      // 如果是今天，只显示时间
      if (date.toDateString() === now.toDateString()) {
        return date.toLocaleTimeString('zh-CN', { 
          hour: '2-digit', 
          minute: '2-digit' 
        });
      }
      
      // 如果是今年，不显示年份
      if (date.getFullYear() === now.getFullYear()) {
        return date.toLocaleDateString('zh-CN', { 
          month: 'short', 
          day: 'numeric' 
        });
      }
      
      // 其他情况显示完整日期
      return date.toLocaleDateString('zh-CN');
    },
    isOverdue(todo) {
      if (!todo.dueDate || todo.completed) return false;
      return new Date(todo.dueDate) < new Date();
    }
  }
}
</script>

<style scoped>
.kanban-view {
  background-color: #f5f5f5;
  border-radius: 8px;
  padding: 20px;
  min-height: 500px;
}

.kanban-columns {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  height: 100%;
}

.kanban-column {
  display: flex;
  flex-direction: column;
  background-color: #ebecf0;
  border-radius: 8px;
  padding: 10px;
}

.column-header {
  padding: 10px;
  border-radius: 4px;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.column-header.todo {
  background-color: #4a90e2;
  color: white;
}

.column-header.in-progress {
  background-color: #f5a623;
  color: white;
}

.column-header.completed {
  background-color: #7ed321;
  color: white;
}

.column-header h3 {
  margin: 0;
  font-size: 1.1rem;
}

.task-count {
  font-size: 0.9rem;
  opacity: 0.9;
}

.column-todos {
  flex: 1;
  min-height: 100px;
  overflow-y: auto;
}

.kanban-card {
  background-color: white;
  border-radius: 4px;
  padding: 12px;
  margin-bottom: 10px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  cursor: move;
  transition: box-shadow 0.2s, transform 0.2s;
}

.kanban-card:hover {
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  transform: translateY(-2px);
}

.kanban-card:active {
  cursor: grabbing;
}

.kanban-card.completed {
  opacity: 0.7;
}

.kanban-card.completed .card-title {
  text-decoration: line-through;
  color: #95a5a6;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 8px;
}

.card-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 500;
  flex: 1;
}

.priority-tag {
  padding: 2px 6px;
  border-radius: 12px;
  font-size: 0.7rem;
  font-weight: bold;
  white-space: nowrap;
}

.priority-tag.high {
  background-color: #ffebee;
  color: #c62828;
}

.priority-tag.medium {
  background-color: #fff8e1;
  color: #f57f17;
}

.priority-tag.low {
  background-color: #e8f5e9;
  color: #2e7d32;
}

.card-content {
  margin-bottom: 10px;
}

.card-description {
  margin: 0;
  font-size: 0.9rem;
  color: #7f8c8d;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.8rem;
}

.due-date {
  color: #7f8c8d;
}

.due-date.overdue {
  color: #e74c3c;
  font-weight: bold;
}

.card-tags {
  display: flex;
  gap: 4px;
}

.tag {
  background-color: #e0e0e0;
  color: #616161;
  padding: 2px 6px;
  border-radius: 12px;
  font-size: 0.7rem;
}

.empty-column {
  text-align: center;
  color: #95a5a6;
  font-style: italic;
  padding: 20px;
  border: 1px dashed #ccc;
  border-radius: 4px;
}

@media (max-width: 768px) {
  .kanban-columns {
    grid-template-columns: 1fr;
    gap: 15px;
  }
  
  .kanban-column {
    min-height: 200px;
  }
}
/* 夜间模式样式 */
.dark-mode .kanban-view {
  background-color: #2d2d2d;
}

.dark-mode .kanban-column {
  background-color: #424242;
}

.dark-mode .column-header.todo {
  background-color: #1976d2;
}

.dark-mode .column-header.in-progress {
  background-color: #f57f17;
}

.dark-mode .column-header.completed {
  background-color: #388e3c;
}

.dark-mode .kanban-card {
  background-color: #333;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

.dark-mode .kanban-card:hover {
  box-shadow: 0 2px 8px rgba(0,0,0,0.4);
}

.dark-mode .card-title {
  color: #e0e0e0;
}

.dark-mode .card-description {
  color: #aaa;
}

.dark-mode .due-date {
  color: #aaa;
}

.dark-mode .due-date.overdue {
  color: #f44336;
}

.dark-mode .tag {
  background-color: #616161;
  color: #e0e0e0;
}

.dark-mode .empty-column {
  color: #aaa;
  border-color: #555;
}

</style>
